
import { Component, Input, OnInit, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { NavController, ModalController } from '@ionic/angular';
import { VstudentinfoService } from '../provider/vstudentinfo.service';
import { ActivatedRoute, Router } from '@angular/router';


@Component({
  selector: 'app-vstudy-view',
  templateUrl: './vstudy-view.component.html',
  styleUrls: ['./vstudy-view.component.scss'],
})
export class VstudyViewComponent implements OnInit, AfterViewInit {
  @ViewChild('video1') myVid: ElementRef;
  @ViewChild('bbb') vName: ElementRef;
  videoUrl: string;
  videoName: string;
  tabs: string = "details";
  isVisble:boolean=true;
  //myVid=document.getElementById("video1"); 
  _endTime: any;
  timer;
  fileUrl = "http://10.84.111.205:21021/browse/UploadFile//aaa//";
  vstudyDetailList: any[];
  attId:number;
  folderName:string;

  constructor(public navController: NavController, public vstudentService: VstudentinfoService, public activatedRoute: ActivatedRoute,public router:Router) { 

  }

   
  ngOnInit() {

    this.videoUrl = this.activatedRoute.snapshot.params['videoUrl'];
    this.videoName = this.activatedRoute.snapshot.params['videoName'];
    this.attId=this.activatedRoute.snapshot.params['attId'];
    this.getVstudyListByAttId();
    this.folderName=this.getFolderName(this.videoName);


  }
  ngAfterViewInit(): void {
    
    this.playMedia(0, 10);
    //this.myVid.nativeElement.addEventListener("timeupdate",this.timeupdate());


  }


  goback1(): void {
    this.navController.back();
  }

  doPressLeft(): void {
     alert("Hi 我在向左滑動");
     this.navController.back();
  }
  doPressRight(): void {
    alert("Hi 我在向右滑動");
  //this.router.navigate(['vsinfo',item],{ queryParams: { 'item': item }});
  this.router.navigate(['tabs/tab1']);
 }
 doPressUp(): void {
  alert("Hi 我在向上滑動");
  this.isVisble=false;
  this.tabs="comments";
}
doPressDown(): void {
  alert("Hi 我在向下滑動");
  this.isVisble=true;
 this.tabs="details";
}

   timeupdate() {
    //因为当前的格式是带毫秒的float类型的如：12.231233，所以把他转成String了便于后面分割取秒  
    var time = this.myVid.nativeElement.currentTime+"";

    var ts = time.substring(0,time.indexOf("."));  
    if (ts == this._endTime) {
      this.myVid.nativeElement.pause();
    }

  }

  //视频播放  
  playMedia(startTime, endTime) {
    //设置结束时间  
    this._endTime = endTime;

    this.myVid.nativeElement.src = this.activatedRoute.snapshot.params['videoUrl'];
    this.myVid.nativeElement.controls = true;
    setTimeout(() => {
      this.setCurrentTime(startTime);

    }, 200);
  }

  //设置视频开始播放事件  
  setCurrentTime(startTime) {
    this.myVid.nativeElement.currentTime = startTime;
    this.myVid.nativeElement.play();
  }

  async getVstudyListByAttId() {

    (await (this.vstudentService.getVStudyDetailList(this.attId))).subscribe((response: any) => this.vstudyDetailList = response.body.result);

  }

  public getFolderName(fileName:string):string 
  {
     let folderName :string;
     folderName=fileName.substring(0,fileName.indexOf('.'))+"\\";
     return folderName;
  }

}

